public class Test {
    public static void main(String[] args) {
        String s = "A man, a plan, a canal: Panama";
        boolean b = isPalindrome(s);
        System.out.println(b);
    }
    //判断是不是字母数字字符
    public static boolean isValidChar(char ch) {
        if ((ch >= 'a' && ch <= 'z') ||
                (ch >= '0' && ch <= '9')) {
            return true;
        }
        return false;
    }
    public static boolean isPalindrome(String s) {
        // 全部换成小写
        s = s.toLowerCase();
        //定义左右两边的下标
        int left = 0, right = s.length() - 1;
        while (left < right) {
            // 1. 从左侧找到一个有效的字符
            while (left < right && !isValidChar(s.charAt(left))) {
                left++;
            }
            // 2. 从右侧找一个有效的字符
            while (left < right && !isValidChar(s.charAt(right))) {
                right--;
            }
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            } else {
                left++;
                right--;
            }
        }
        return true;
    }
}
